<?php

namespace app\admin\controller\content;

use app\common\controller\Backend;
use app\model\Article as ArticleModel;
/**
 * 文章管理
 *
 * @icon  fa fa-pencil-square
 */
class Article extends Backend
{

    /**
     * 文章列表
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,null);

            $list = db('Article')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

//            $data = $list->items();

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend"=>[]);

            return json($result);
        }
        $this->view->assign('shangjia_tag',__('On Off'));
        return $this->view->fetch();

    }

    /**
     * 添加文章
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'Article');
            if($msg != 'true')
                $this->error($msg);


            if(empty($params['type'])){
                $this->error(__('Please choose the type'));
            }
            $params['create_time'] = $params['update_time'] = date("Y-m-d H:i:s");
            if(empty($params['descript'])){
                $params['descript'] = '';
            }

            unset($params['id']);
            db('article')->insertGetId($params);
            $this->success(__('Add Success'));
        }

        $articleModelHandle = new ArticleModel();
        $typeList = $articleModelHandle->getTypeList();
        $this->view->assign('thumb',"/assets/img/circle.png");
        $this->view->assign('thumb2',"/assets/img/circle.png?caijian=all");
        $this->view->assign('id',0);
        $this->view->assign('data',[]);
        $this->view->assign('type',0);
        $this->view->assign('typeList',$typeList);
        return $this->view->fetch();

    }

    /**
     * 编辑文章
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'Article');
            if($msg != 'true')
                $this->error($msg);

            if(empty($params['type'])){
                $this->error(__('Please choose the type'));
            }
            if($params['id'] <= 3){
                unset($params['type']);
            }

            if(empty($params['descript'])){
                $params['descript'] =  '';
            }
            $params['update_time'] = date("Y-m-d H:i:s");
            db('article')->update($params);
            $this->success(__('Operation Success'));
        }

        $articleModelHandle = new ArticleModel();
        $typeList = $articleModelHandle->getTypeList();
        $data = db('article')->find($ids);
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        $this->view->assign('type',$data['type']);
        $this->view->assign('typeList',$typeList);
        return $this->view->fetch('add');
    }

    /**
     * 删除文章
     */
    public function del($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        $ids = explode(',',$ids);
        foreach ($ids as $id){
            if($id <= 3){
                continue;
            }
            db('article')->where('id',$id)->delete();

        }
        $this->success(__('Operation Success'));
    }

    /**
     * 状态修改
     */
    public function uparticle($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        $ids = explode(',',$ids);
        foreach ($ids as $id){
            $findOne = db('article')->find($id);
            if(empty($findOne) || $findOne['type'] == 1){
                continue;
            }
            $up = [];
            $up['status'] = $findOne['status'] == 1 ? -1 : 1;
            db('article')->where('id',$id)->update($up);
        }

        $this->success(__('Operation Success'));
    }
}
